package com.fdb.efp.console.executor.util;

import com.alibaba.dubbo.rpc.service.GenericService;
import com.fdb.basic.framework.core.util.HsfGenericUtils;
import com.fdb.basic.framework.core.util.SpringContextUtils;
import com.fdb.basic.framework.core.util.StringUtil;
import com.fdb.efp.console.common.constance.FlowConstance;
import com.fdb.efp.console.common.vo.EngineVO;
import com.fdb.efp.console.service.vo.BizFlowTaskParamVO;
import com.fdb.efp.console.service.vo.InstFlowTaskVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.Objects;

/**
 * flow 中阿里泛型服务调用
 * 
 * @author Cytus_
 * @since 2018年6月1日 下午2:08:22
 * @version 1.0
 *
 */
public class AliGenericServiceCallUtil {

    private static Logger logger = LoggerFactory.getLogger(AliGenericServiceCallUtil.class);
    
    @SuppressWarnings("rawtypes")
    public static void aliGenericServiceCall(EngineVO evo, InstFlowTaskVO task, String[] paramTyps, Object[] params, List<BizFlowTaskParamVO> paramVos) throws Exception {

        //泛型调用服务方法，如果存在出参的话，使用对象接受，不存在的话，则忽略服务出参

        GenericService gs = (GenericService) SpringContextUtils.getBean(task.getBizServiceClass());
        SpringContextUtils.getBean(task.getBizServiceClass());
        logger.info("节点实例号："+ evo.getNodeInstanceId() +", 任务编号："+ task.getTaskId() +", 调用的服务插件为："+ task.getBizServiceClass() +", 服务方法为:"+ task.getBizPluginMethodName());
        Object rtnObject = gs.$invoke(task.getBizPluginMethodName(), paramTyps, params);
        //泛型返回对象数据转换
        Object info = null;
        if (Objects.nonNull(rtnObject)) {
            info = HsfGenericUtils.exchangeRtnData(rtnObject);
        }
        
        BizFlowTaskParamVO outParam = null;
        if (!paramVos.isEmpty()) {
            //判断配置出参数量，大于1个则通过配置的CLASS类进行匹配
            if (paramVos.size() > 1) {
                logger.warn("当前上下文存在默认参数, 对应的任务ID:"+ evo.getTaskId() +"对应的服务插件："+ task.getBizPluginId() +"所配置的出参参数多于一个, 将进行默认匹配!");
                for (BizFlowTaskParamVO paramVo : paramVos) {
                    String[] paramClasses = paramVo.getParamClass().split("@");
                    if (paramClasses.length == 1 && paramVo.getParamClass().equals(info.getClass().getName())) {
                        outParam = paramVo;
                        break;
                    } else if (paramClasses.length == 2 && info instanceof List) {
                        List list = (List) info;
                        if (list.isEmpty()) {
                            outParam = paramVo;
                            break;
                        } else if (paramClasses[0].equals(list.getClass().getName()) && paramClasses[1].equals(list.get(0).getClass().getName())){
                            outParam = paramVo;
                            break;
                        }
                    }
                }
            } else {
                outParam = paramVos.get(0);
            }
        }
        if (Objects.nonNull(outParam) && StringUtil.isNotEmpty(outParam.getBizTargetParamCode())) {
            logger.info("当前任务ID:"+ evo.getTaskId() +"对应的服务插件："+ task.getBizPluginId() +"所配置代码为："+ outParam.getBizTargetParamCode() +"!");
            evo.getPluginVo().addVOs(outParam.getBizTargetParamCode(), info);
            evo.addExeParams(FlowConstance.LAST_PLUGIN_OUT_PARAM_KEY, info);
        }
    }
    
}
